﻿using 示例中体现的ModelBus核心思想.Models.Domain;
using 示例中体现的ModelBus核心思想.Models.View;

namespace 示例中体现的ModelBus核心思想.ModelBus.Converters
{
    /// <summary>
    /// 领域模型 → 视图模型 转换器
    /// 负责将业务模型转换为适合前端展示的格式
    /// </summary>
    public class DomainToViewConverter : IModelConverter<Product, ProductView>
    {
        public ProductView Convert(Product source)
        {
            if (source == null) return null;

            return new ProductView
            {
                Id = source.Id,
                Name = source.Name,
                Price = source.Price,
                // 转换为前端友好的状态文本
                StatusText = source.Status switch
                {
                    ProductStatus.Draft => "草稿",
                    ProductStatus.Active => "在售",
                    ProductStatus.Disabled => "下架",
                    _ => "未知"
                },
                // 调用领域行为获取计算结果
                IsAvailable = source.IsAvailable()
            };
        }

        public IEnumerable<ProductView> Convert(IEnumerable<Product> sources)
        {
            return sources?.Select(Convert) ?? Enumerable.Empty<ProductView>();
        }
    }
}
